package com.timoon.webcomponent.utility.sift.jd;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;

/**
 *<p>仿京东的商品筛选面板</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public abstract class JdSiftPanel extends Panel {

	private JdSiftService service;
	
	private Integer goodsId;
	
	private List<JdSiftModel> selectedPool=new ArrayList<>();
	
	private WebMarkupContainer selectedContainer;
	
	/**
	 * 
	 * @param id
	 * @param goodsId 传入的商品id，用户选中此商品，筛选条件从此商品上挖掘
	 * @param targetPage
	 * @param service
	 */
	public JdSiftPanel(String id,Integer goodsId,JdSiftService service) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.service=service;
		
		this.goodsId=goodsId;
		
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderGrid();
		
		renderSelectedContainer();
	}

	private void renderSelectedContainer() {
		// TODO Auto-generated method stub
		
		selectedContainer=new WebMarkupContainer("wk_selected_container");
		selectedContainer.setOutputMarkupId(true);
		add(selectedContainer);
		
		renderSelectedList(null);
	}

	private void renderSelectedList(AjaxRequestTarget target) {
		// TODO Auto-generated method stub
		ListView<JdSiftModel> listView=new ListView<JdSiftModel>("wk_selected",selectedPool) {
			
			@Override
			protected void populateItem(ListItem<JdSiftModel> arg0) {
				// TODO Auto-generated method stub
				final JdSiftModel model=arg0.getModelObject();
				
				AjaxLink link=new AjaxLink("wk_selected_link") {

					@Override
					public void onClick(AjaxRequestTarget arg0) {
						// TODO Auto-generated method stub
						selectedPool.remove(model);
						renderSelectedList(arg0);
					}
				};
				arg0.add(link);
				
				link.add(new Label("wk_selected_label",model.getTitle()));
			}
		};
		selectedContainer.addOrReplace(listView);
		
		if(target!=null){
			target.add(selectedContainer);
			
			onSelectedPoolChange(selectedPool,target);
		}
	}

	private void renderGrid() {
		// TODO Auto-generated method stub
		
		List<JdSiftModel> models=this.service.getPropertyListByGoodsId(goodsId);
		
		ListView<JdSiftModel> listView=new ListView<JdSiftModel>("wk_list",models) {
			
			@Override
			protected void populateItem(ListItem<JdSiftModel> arg0) {
				// TODO Auto-generated method stub
				
				JdSiftModel model=arg0.getModelObject();
				arg0.add(new Label("wk_title",model.getTitle()));
				
				ListView<JdSiftModel> list2=new ListView<JdSiftModel>("wk_li",model.getItems()) {
					
					@Override
					protected void populateItem(ListItem<JdSiftModel> arg0) {
						// TODO Auto-generated method stub
						
						final JdSiftModel model=arg0.getModelObject();
						
						AjaxLink link=new AjaxLink("wk_link"){

							@Override
							public void onClick(AjaxRequestTarget arg0) {
								// TODO Auto-generated method stub
								
								if(selectedPool.contains(model)){
									selectedPool.remove(model);
								}else{
									selectedPool.add(model);
								}
								
								renderSelectedList(arg0);
							}};
						arg0.add(link);
						
						link.add(new Label("wk_label",model.getTitle()));
					}
				};
				arg0.add(list2);
			}
		};
		add(listView);
	}
	
	/**当筛选条件发生改变时触发此方法*/
	protected abstract void onSelectedPoolChange(List<JdSiftModel> selectedPool2,AjaxRequestTarget target);
}
